﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SAASInvest.Contracts;
using System.ServiceModel;

namespace SAASInvest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        #region Button Click

        private void IISUploadButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("Please select file!");
                return;
            }
            if (!File.Exists(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("The selected file is not exist, Please select again!");
                return;
            }
            //using (ServiceReference1.ControllerClient client = new ServiceReference1.ControllerClient())
            //{
            //    string fileFullName = ToUploadFileTextBox.Text;
            //    int index = fileFullName.LastIndexOf("\\");
            //    string fileName = fileFullName.Substring(index + 1);
            //    using (FileStream stream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
            //    {
            //        client.UploadFile(fileName, "", stream);
            //        stream.Close();
            //    }
            //}
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SelectFileButton_Click(object sender, EventArgs e)
        {
            if (UploadOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ToUploadFileTextBox.Text = UploadOpenFileDialog.FileName;
            }
        }

        #endregion

        private void ConsoleUploadButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("Please select file!");
                return;
            }
            if (!File.Exists(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("The selected file is not exist, Please select again!");
                return;
            }
            try
            {
                ChannelFactory<IController> channelFacotry = (new ChannelFactory<IController>("ConsoleController"));
                IController controller = channelFacotry.CreateChannel();
                string fileFullName = ToUploadFileTextBox.Text;
                int index = fileFullName.LastIndexOf("\\");
                string fileName = fileFullName.Substring(index + 1);
                using (FileStream stream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
                {
                    controller.UploadFile(new FileUploadStream() { UploadStream = stream, FileName = fileName });
                }
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message);
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            return;
            if (String.IsNullOrEmpty(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("Please select file!");
                return;
            }
            if (!File.Exists(ToUploadFileTextBox.Text))
            {
                MessageBox.Show("The selected file is not exist, Please select again!");
                return;
            }
            //backgroundWorker1.RunWorkerAsync();
            IAsynFileUpload client = (new ChannelFactory<IAsynFileUpload>("WSHttpBinding_IAsynFileUpload1")).CreateChannel();
            {
                string fileFullName = ToUploadFileTextBox.Text;
                int index = fileFullName.LastIndexOf("\\");
                string fileName = fileFullName.Substring(index + 1);
                FileUploadData fileUploadData = client.GetFile(fileName);
                if (fileUploadData == null)
                {
                    fileUploadData = new FileUploadData() { FileName = fileName };
                }
                using (FileStream stream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
                {
                    fileUploadData.Length = stream.Length;

                    long count = 0;
                    while (fileUploadData.Offset != fileUploadData.Length)
                    {
                        count = ((fileUploadData.Length - fileUploadData.Offset) > Constants.MaxLengthPerRead ? Constants.MaxLengthPerRead : (fileUploadData.Length - fileUploadData.Offset));
                        fileUploadData.UploadData = new byte[count];
                        stream.Position = fileUploadData.Offset;
                        stream.Read(fileUploadData.UploadData, 0, fileUploadData.UploadData.Length);
                        fileUploadData.DataCount = (int)count;
                        fileUploadData=client.AnysUploadFile(fileUploadData); 
                    } 
                    stream.Close();
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        { 
            using (ServicesHost.AsynFileUploadClient client = new ServicesHost.AsynFileUploadClient())
            {
                string fileFullName = ToUploadFileTextBox.Text;
                int index = fileFullName.LastIndexOf("\\");
                string fileName = fileFullName.Substring(index + 1);
                FileUploadData file = client.GetFile(fileName);
                if (file == null)
                {
                    file = new FileUploadData() { FileName = fileName };
                }
                using (FileStream stream = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
                {
                    int maxSize = 102400;
                    file.Length = stream.Length;
                    while (file.Length != file.Offset)
                    {
                        file.UploadData = new byte[file.Length - file.Offset <= maxSize ? file.Length - file.Offset : maxSize];
                        stream.Position = file.Offset;
                        stream.Read(file.UploadData, 0, file.UploadData.Length);
                        file = client.AnysUploadFile(file);

                        e.Result = file.Offset;
                        (sender as BackgroundWorker).ReportProgress((int)(((double)file.Offset / (double)((long)file.Length)) * 100), file.Offset);

                        if (this.backgroundWorker1.CancellationPending)
                            return;
                    }
                    stream.Close();
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //MessageBox.Show("Upload Successful!");
        }

    }
}
